package com.dz.Incubator.controller;

import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.config.ReturnCode;
import com.dz.Incubator.entity.InvokeResult;
import com.dz.Incubator.entity.ProblemInfoEntity;
import com.dz.Incubator.service.ProblemInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/a/zx/problemInfo")
@Slf4j
public class ProblemInfoController {


    @Autowired
    private ProblemInfoService problemInfoService;

    /**
     * 分页查询 problemInfo 对象
     *
     */
    @RequestMapping(value = "/getPageList", method = RequestMethod.POST)
    @ResponseBody
    public InvokeResult<Pagination> getPageList(@RequestBody Pagination pagination) {
        InvokeResult invokeResult;
        try {
            if (null == pagination) {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
            Pagination pageInfo = problemInfoService.findByPagination(pagination);
            if (null != pageInfo && null != pageInfo.getList() && pageInfo.getList().size() > 0) {
                invokeResult = InvokeResult.success(pageInfo);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 获取单个ProblemInfoEntity 对象
     *
     * @param id Id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<ProblemInfoEntity> get(@PathVariable Long id) {
        InvokeResult invokeResult;
        try {
            if (null != id) {
                ProblemInfoEntity problemInfoEntity = problemInfoService.get(id);
                if (null == problemInfoEntity) {
                    invokeResult = InvokeResult.pass_no_data();
                }
                invokeResult = InvokeResult.success(problemInfoEntity);
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 删除单个problemInfoEntity 对象
     *
     * @param id 删除数据 ID
     */
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult delete(@RequestParam Long id) {
        InvokeResult invokeResult = null;
        try {
            if (null != id) {
                problemInfoService.delete(id);
                invokeResult = InvokeResult.success();
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 集合查找 problemInfoEntity 对象
     *
     */
    @RequestMapping(value = "/find", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<List<ProblemInfoEntity>> find(@RequestParam("ids") List<Long> ids) {
        InvokeResult invokeResult = null;
        try {
            if (null != ids && ids.size() > 0) {
                Long[] skuIds = ids.toArray(new Long[ids.size()]);
                List<ProblemInfoEntity> problemInfoEntitys = problemInfoService.find(skuIds);
                if (null != problemInfoEntitys && problemInfoEntitys.size() > 0) {
                    invokeResult = InvokeResult.success(problemInfoEntitys);
                } else {
                    invokeResult = InvokeResult.pass_no_data();
                }
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 查找所有 problemInfoEntity 对象
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<ProblemInfoEntity>> findAll() {
        InvokeResult invokeResult = null;
        try {
            List<ProblemInfoEntity> problemInfoEntitys = problemInfoService.findAll();
            if (null != problemInfoEntitys && problemInfoEntitys.size() > 0) {
                invokeResult = InvokeResult.success(problemInfoEntitys);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 新增数据
     *
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<ProblemInfoEntity> add(@RequestBody ProblemInfoEntity problemInfoEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null != problemInfoEntity) {
                invokeResult = InvokeResult.success(problemInfoService.insert(problemInfoEntity));
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 修改数据
     *
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<ProblemInfoEntity> update(@RequestBody ProblemInfoEntity problemInfoEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null != problemInfoEntity && null != problemInfoEntity.getId()) {
                invokeResult = InvokeResult.success(problemInfoService.update(problemInfoEntity));
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }


    @RequestMapping(value = "findPrevAndNext/{id}",method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<Map<String,ProblemInfoEntity>> findPrevAndNext(@PathVariable int id){
        return InvokeResult.success(problemInfoService.findPrevAndNext(id));
    }
}